﻿using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DaveSexton.Labs
{
	public abstract partial class SilverlightConsoleLab : SilverlightLab, IGUIConsoleLab
	{
		#region Public Properties
		public static readonly DependencyProperty IsRunningProperty = DependencyProperty.Register("IsRunning", typeof(bool), typeof(SilverlightConsoleLab), new PropertyMetadata(false));
		public static readonly DependencyProperty WaitingForUserInputProperty = DependencyProperty.Register("WaitingForUserInput", typeof(bool), typeof(SilverlightConsoleLab), new PropertyMetadata(false));
		public static readonly DependencyProperty HasExperimentsProperty = DependencyProperty.Register("HasExperiments", typeof(bool), typeof(SilverlightConsoleLab), new PropertyMetadata(false));
		public static readonly DependencyProperty ExperimentsProperty = DependencyProperty.Register("Experiments", typeof(IList<IExperiment>), typeof(SilverlightConsoleLab), new PropertyMetadata(null));
		public static readonly DependencyProperty SelectedExperimentProperty = DependencyProperty.Register("SelectedExperiment", typeof(IExperiment), typeof(SilverlightConsoleLab), new PropertyMetadata(null, SelectedExperiment_Changed));
		public static readonly DependencyProperty PauseBetweenExperimentsProperty = DependencyProperty.Register("PauseBetweenExperiments", typeof(bool), typeof(SilverlightConsoleLab), new PropertyMetadata(true));
		public static readonly DependencyProperty StartButtonTextProperty = DependencyProperty.Register("StartButtonText", typeof(string), typeof(SilverlightConsoleLab), new PropertyMetadata(Lab.StartButtonTextDefault));

		public string StartButtonText
		{
			get
			{
				return (string) GetValue(StartButtonTextProperty);
			}
			set
			{
				SetValue(StartButtonTextProperty, value);
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="SilverlightConsoleLab" /> class for derived classes.
		/// </summary>
		protected SilverlightConsoleLab()
			: base(traceHeader: false)
		{
			Contract.Ensures(!HasProxy);

			InitializeComponent();

			Contract.Assume(StartButton != null);
			Contract.Assume(InputTextBox != null);

			Unloaded += Lab_Unloaded;

			StartButton.Click += StartButton_Click;
			InputTextBox.KeyUp += InputTextBox_KeyUp;
			InputTextBox.AddHandler(TextBox.TextInputEvent, new TextCompositionEventHandler(InputTextBox_TextInput), handledEventsToo: true);
		}
		#endregion

		#region Methods
		[ContractInvariantMethod]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
		private void ObjectInvariant()
		{
			Contract.Invariant(StartButton != null);
			Contract.Invariant(InputTextBox != null);
		}

		partial void ExperimentSelected(IExperiment experiment)
		{
			Contract.Requires(experiment != null);

			StartButtonText =
				!HasExperiments || experiment == null || IsMainExperiment(experiment)
				? Lab.StartButtonTextDefault
				: Lab.StartButtonTextExperiment;
		}
		#endregion
	}
}